package cn.buk.qms;

import cn.buk.common.util.DateUtil;
import cn.buk.tms.entity.misc.FlightInfo;
import cn.buk.tms.entity.order.flight.*;
import cn.buk.tms.entity.order.train.TrainOrder;
import cn.buk.tms.entity.order.train.TrainOrderPassenger;
import cn.buk.tms.entity.order.vas.VasOrder;
import cn.buk.tms.entity.order.vas.VasOrderFlight;

import java.text.ParseException;
import java.util.Date;

/**
 * @author yfdai
 */
public class SmsUtil {

    public static String convertFlightOrder(FlightOrder flightOrder) {
        String val = null;
        for(FlightOrderFlight flt: flightOrder.getFlights()) {
            if (val == null) {
                val = convertFlightInfo(flt.getFlight());
            } else {
                val += convertFlightInfo(flt.getFlight());
            }
        }

        return val;
    }

    private static String convertFlightInfo(FlightInfo info) {
        String[] items = info.getDepartureDate().split("-");
        String retVal = items[0] + "年" + items[1] + "月" + items[2] + "日";

        retVal += info.getDepartureAirportName();
        if (info.getDepartureTerminal().length() > 0) {
            retVal += "(" + info.getDepartureTerminal() + ")";
        }
        retVal += "到" + info.getArrivalAirportName();
        if (info.getArrivalTerminal().length() > 0) {
            retVal += "(" + info.getArrivalTerminal() + ")";
        }

        retVal += "，航班号:" + info.getFlightNo();
        if (info.getAirlineName().length() > 0) {
            retVal += "(" + info.getAirlineName() + ")";
        }
        retVal += "，";

        if (info.getCabinType() > 0) {
            switch (info.getCabinType()) {
                case 10:
                    retVal += "头等舱(" + info.getSubclass() + ")，";
                    break;
                case 20:
                    retVal += "商务舱(" + info.getSubclass() + ")，";
                    break;
                case 30:
                    retVal += "经济舱(" + info.getSubclass() + ")，";
                    break;
                case 40:
                    retVal += "超级经济舱(" + info.getSubclass() + ")，";
                    break;
                default:
                    break;
            }
        }

        retVal += convertTimeFormat(info.getDepartureTime()) + "出发";

        retVal += convertTimeFormat(info.getArrivalTime());
        if(info.getArrivalDate() != null && info.getArrivalDate().trim().length() == 10 && !info.getDepartureDate().equalsIgnoreCase(info.getArrivalDate())) {
            retVal += "(" + info.getArrivalDate() + ")";
        }
        retVal += "到达";

        return retVal;
    }

    public static String convertFlightOrderPsgInfo(FlightOrder flightOrder) {
        String val = null;
        for(FlightOrderPassenger psg: flightOrder.getPassengers()) {
            if (val == null) {
                val = psg.getName() + "(" + psg.getIdNo() + ")";
            } else {
                val += psg.getName() + "(" + psg.getIdNo() + ")";
            }
        }

        return val;
    }

    private static String convertTimeFormat(String val) {
        if (val == null) {
          return "";
        }
        val = val.trim();
        if (val.length() == 5) {
          return val;
        }
        if (val.length() < 4) {
          return val;
        }

        return val.substring(0, 2) + ":" + val.substring(2);
    }

    public static String convertRefundOrder(FlightRefundOrder order) {
        String val = null;
        for(FlightRefundOrderDetail flt: order.getFlights()) {
            if (val == null) {
                val = convertFlightInfo(flt.getFlight());
            } else {
                val += convertFlightInfo(flt.getFlight());
            }
        }

        return val;
    }

    public static String convertChangeOrder(FlightChangeOrder order) {
        String val = null;
        for(FlightChangeOrderDetail flt: order.getFlights()) {
            if (flt.getFlightType() == 0) {
              continue;
            }
            //只保留改签后的航班信息
            if (val == null) {
                val = convertFlightInfo(flt.getFlight());
            } else {
                val += convertFlightInfo(flt.getFlight());
            }
        }

        return val;
    }

    public static String convertTrainOrder(TrainOrder order, TrainOrderPassenger psgInfo) {

        return psgInfo.getName() + "于" + DateUtil.formatDate(order.getDdate(), "yyyy年MM月dd日")
                + order.getTrainNo() + "次" + psgInfo.getCoachNo() + "车" + psgInfo.getSeatNo() + "号(" + order.getSeatClass() + ")，"
                + order.getDcity() + order.getDtime() + "开-" + order.getAcity() + order.getAtime() + "到";
    }

    public static String convertVaasOrder(VasOrder order) {
        //{{01月24日上海浦东至大邱MU5053}}
        //机场登机牌订单只包含一个航段
        for (VasOrderFlight flight : order.getFlights()) {
            try {
                final Date ddate = DateUtil.convertToDate(flight.getFlight().getDepartureDate(), "yyyy-MM-dd");

                return DateUtil.formatDate(ddate, "yyyy年MM月dd日")
                        + flight.getFlight().getDepartureAirportName() + "至"
                        + flight.getFlight().getArrivalAirportName()
                        + flight.getFlight().getFlightNo();
            } catch (ParseException e) {
                throw new RuntimeException("(" + flight.getFlight().getDepartureDate() + ") is an invalid date.");
            }
        }

        throw new RuntimeException("VaasOrder has not flights.");
    }

}
